﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Alos.Common;
using Alos.Entity;
using Alos.IService;
using System.Threading.Tasks;
using Alos.Entity.Dto;
using Newtonsoft.Json;

namespace Alos.LanClass.Api
{
    /// <summary>
    /// 用户指标获取
    /// </summary>
    public class ReTestController : ApiController
    {
        IReExamDetailService ReExamDetailService;
        IReExamSubmitService ReExamSubmitService;
        /// <summary>
        /// 构造函数
        /// </summary>
        public ReTestController(IReExamDetailService _ReExamDetailService, IReExamSubmitService _ReExamSubmitService)
        {
            ReExamDetailService = _ReExamDetailService;
            ReExamSubmitService = _ReExamSubmitService;
        }
        /// <summary>
        /// 保存试题答案
        /// </summary>
        /// <param name="examData"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/ReTest/SaveExamResult")]
        public JsonRtn<dynamic> SaveExamResult(RxamDetailInfo examData)
        {
            var jsRtn = new JsonRtn<dynamic>();
            try
            {
                var _es = JsonConvert.DeserializeObject<ReExamSubmit>(examData.es);
                _es.StudentID = Common.Current.User.id;
                var _led = JsonConvert.DeserializeObject<List<ReExamDetail>>(examData.ed);
                _led.ForEach(T =>
                {
                    T.StudentID = Convert.ToInt32(Common.Current.User.id);
                });
                if (ReExamSubmitService.Add(_es) && ReExamDetailService.Add(_led))
                {
                    foreach (ReExamDetail item in _led)
                    {
                        if (item.QuestionTypeCode == "QUE0042")
                        {
                            //UploadPhotoToFtp(item.photoFullPath);
                        }
                    }
                    jsRtn.state = RtnState.sucess;
                    jsRtn.message = "答案提交成功！";

                }
                else
                {
                    jsRtn.state = RtnState.fail;
                    jsRtn.message = "服务器繁忙请稍候再试！";
                }
            }
            catch (Exception ex)
            {

            }
            return jsRtn;
        }
    }
}
